package com.junbao.message.infrastructure.result;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;

public class Result {

	private String result_code;
	
	private String result_msg;
	
	private Map<String, String> params = new HashMap<String, String>();
	
	public static final Result SUCCESS = new Result("0000", "业务处理成功！");
	public static final Result REQUEST_DATA_NULL = new Result("1001", "请求参数为空！");
	public static final Result MESSAGE_NO_EXIST = new Result("0000", "消息编号已存在！");
	public static final Result QUERY_MESSAGE_FAIL_BY_MESSAGESQUECE = new Result("1002", "根据序列号查询消息信息为空！");
	
	public Result(String result_code, String result_msg) {
		this.result_code = result_code;
		this.result_msg = result_msg;
	}
	
	public String getResult_code() {
		return result_code;
	}

	public void setResult_code(String result_code) {
		this.result_code = result_code;
	}

	public String getResult_msg() {
		return result_msg;
	}

	public void setResult_msg(String result_msg) {
		this.result_msg = result_msg;
	}

	public Map<String, String> getParams() {
		return params;
	}

	public void setParams(Map<String, String> params) {
		this.params = params;
	}
	
	public Result putData(String key,String value){
		if (StringUtils.isEmpty(key)||StringUtils.isEmpty(value)) {
			throw new NullPointerException("添加参数错误，添加参数不可为空！");
		}
		this.params.put(key, value);
		return this;
	}
	
	public String buildResultMsg(){
		Map<String, String> returnParm = new LinkedHashMap<>();
		JSONObject returnMsg = JSONObject.fromObject(returnParm);
		returnMsg.put("result_code", this.result_code);
		returnMsg.put("result_msg", this.result_msg);
		for (Entry<String, String> param : this.params.entrySet()) {
			returnMsg.put(param.getKey(), param.getValue());
		}
		return returnMsg.toString();
	}
	
	public static void main(String[] args) {
		Result result = Result.SUCCESS;
		result.putData("message_no", "123456");
		System.out.println(result.buildResultMsg());
	}
}
